package cn.icanci.ladybird.common.util.file;

import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.Properties;

/**
 * @author: icanci
 * @date: Created in 2020/10/5 4:36
 * @classAction: ftp服务器工具类
 */
@Component
@Slf4j
public class FtpUtil {
    /**
     * ftp服务器ip地址
     */
    private static final String HOST = "47.112.124.138";

    /**
     * 端口
     */
    private static final int PORT = 22;
    /**
     * 用户名
     */
    private static final String USER_NAME = "root";

    /**
     * 密码
     */
    private static final String PASSWORD = "NkHcAmSWHVUCvKG8mGLEAn";

    /**
     * 存放图片的根目录
     */
    public static final String ROOT_PATH = "/usr/local/oos/webapps/ladybird-web-static/upload-info/";

    /**
     * 存放图片的路径
     */
    public static final String IMG_URL = "https://oos.icanci.cn/app/static/webapps/ladybird-web-static/upload-info";


    /**
     * 获取流
     *
     * @return 返回ChannelSftp
     * @throws Exception
     */
    private static ChannelSftp getChannel() throws Exception {
        JSch jsch = new JSch();
        //->ssh root@host:port
        Session sshSession = jsch.getSession(USER_NAME, HOST, PORT);
        //密码
        sshSession.setPassword(PASSWORD);
        Properties sshConfig = new Properties();
        sshConfig.put("StrictHostKeyChecking", "no");
        sshSession.setConfig(sshConfig);
        sshSession.connect();
        Channel channel = sshSession.openChannel("sftp");
        channel.connect();
        return (ChannelSftp) channel;
    }

    /**
     * ftp上传图片
     *
     * @param inputStream 图片io流
     * @param imagePath   路径，不存在就创建目录
     * @param imagesName  图片名称
     * @return urlStr 图片的存放路径
     */
    public static String putImages(InputStream inputStream, String imagePath, String imagesName) {
        try {
            ChannelSftp sftp = getChannel();
            String path = ROOT_PATH + imagePath + "/";
            createDir(path, sftp);
            //上传文件
            sftp.put(inputStream, path + imagesName);
            log.info("上传成功！");
            sftp.quit();
            sftp.exit();
            //处理返回的路径
            String resultFile;
            resultFile = IMG_URL + imagePath + imagesName;
            return resultFile;
        } catch (Exception e) {
            log.error("上传失败：" + e.getMessage());
        }
        return "";
    }

    /**
     * 创建目录
     */
    private static void createDir(String path, ChannelSftp sftp) throws SftpException {
        String[] folders = path.split("/");
        sftp.cd("/");
        for (String folder : folders) {
            if (folder.length() > 0) {
                try {
                    sftp.cd(folder);
                } catch (SftpException e) {
                    sftp.mkdir(folder);
                    sftp.cd(folder);
                }
            }
        }
    }

    /**
     * 删除图片
     */
    public static void delImages(String imagesName) {
        try {
            ChannelSftp sftp = getChannel();
            String path = ROOT_PATH + imagesName;
            sftp.rm(path);
            sftp.quit();
            sftp.exit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
